//app.js
import {ajax} from '/utils/httpRequest.js'
import { URI_LOGIN} from '/api.js'
App({
  onLaunch: function () {
    wx.login({
      success:(res)=>{
        // 发送 res.code 到后台换取 openId, sessionKey, unionId
        var sysInfo,sendCode;
        wx.getSystemInfo({
          success: function (res) {
            sysInfo = res;
            //console.log(res)
            if (res.platform && (res.platform.indexOf('ios') > -1 ||
              res.system.indexOf('iOS') > -1)) {
              sysInfo.platform = 1;
            } else {
              sysInfo.platform = 2;
            }
          }
        });
        // 发送 res.code 到后台换取 openId, sessionKey, unionId
        sendCode= res.code;
        ajax.post(URI_LOGIN, {
          code: sendCode,
          model: sysInfo.model,
          vendor: sysInfo.brand,
          osVersion: sysInfo.system,
          platform: sysInfo.platform,
          appVersion: sysInfo.version
        }, {}, (res) => {
          //console.log(res.data.data);
          let userInfo = res.data.data;
          wx.setStorageSync('accessToken', userInfo.accessToken);
          wx.setStorageSync('accessUserId', userInfo.id);
          /**** ****/
          wx.setStorage({key: "zthUserInfo", data: userInfo});
          // if (!userInfo.nickName || !userInfo.headUrl) {
          //   //授权获取用户头像给服务器
          //   wx.getUserInfo({
          //     success: res => {
          //       console.log(res);
          //       wx.setStorageSync('userInfo', res.userInfo)
          //       ajax.post(URI_POSTUSERINFO, {
          //         nickName: res.userInfo.nickName,
          //         headUrl: res.userInfo.avatarUrl,
          //         accessToken: wx.getStorageSync('accessToken')
          //       }, {}, (res) => {
          //         console.log(res);
          //       })
          //     }
          //   });
          // }
        })
      }
    })
  },
  globalData: {
    userInfo: null
  }
})